#include "stdafx.h"



#if defined(QSQL)


#include "qsql.h"
using namespace micro::qsql;


/////////////////////////////////////////////////////////////////////////////
// QsqlError

QsqlError::QsqlError( const int nErrCode, LPTSTR szErrMess,	bool bDeleteMsg/*=true*/ ) :	mnErrCode( nErrCode )
{

    mpszErrMess.Format( _T( "%s[%d]: %s" ),
                        errorCodeAsString( nErrCode ),
                        nErrCode,
                        szErrMess ? szErrMess : _T( "" ) );

    if ( bDeleteMsg && szErrMess )
    {
        _sqlite3_free( ( char * )szErrMess );
    }
}


QsqlError::QsqlError( const QsqlError & e ) :	mnErrCode( e.mnErrCode )
{

    mpszErrMess = e.mpszErrMess ;

}


LPCTSTR QsqlError::errorCodeAsString( int nErrCode )
{
    switch ( nErrCode )
    {
        case SQLITE_OK          :
            return _T( "SQLITE_OK" );

        case SQLITE_ERROR       :
            return _T( "SQLITE_ERROR" );

        case SQLITE_INTERNAL    :
            return _T( "SQLITE_INTERNAL" );

        case SQLITE_PERM        :
            return _T( "SQLITE_PERM" );

        case SQLITE_ABORT       :
            return _T( "SQLITE_ABORT" );

        case SQLITE_BUSY        :
            return _T( "SQLITE_BUSY" );

        case SQLITE_LOCKED      :
            return _T( "SQLITE_LOCKED" );

        case SQLITE_NOMEM       :
            return _T( "SQLITE_NOMEM" );

        case SQLITE_READONLY    :
            return _T( "SQLITE_READONLY" );

        case SQLITE_INTERRUPT   :
            return _T( "SQLITE_INTERRUPT" );

        case SQLITE_IOERR       :
            return _T( "SQLITE_IOERR" );

        case SQLITE_CORRUPT     :
            return _T( "SQLITE_CORRUPT" );

        case SQLITE_NOTFOUND    :
            return _T( "SQLITE_NOTFOUND" );

        case SQLITE_FULL        :
            return _T( "SQLITE_FULL" );

        case SQLITE_CANTOPEN    :
            return _T( "SQLITE_CANTOPEN" );

        case SQLITE_PROTOCOL    :
            return _T( "SQLITE_PROTOCOL" );

        case SQLITE_EMPTY       :
            return _T( "SQLITE_EMPTY" );

        case SQLITE_SCHEMA      :
            return _T( "SQLITE_SCHEMA" );

        case SQLITE_TOOBIG      :
            return _T( "SQLITE_TOOBIG" );

        case SQLITE_CONSTRAINT  :
            return _T( "SQLITE_CONSTRAINT" );

        case SQLITE_MISMATCH    :
            return _T( "SQLITE_MISMATCH" );

        case SQLITE_MISUSE      :
            return _T( "SQLITE_MISUSE" );

        case SQLITE_NOLFS       :
            return _T( "SQLITE_NOLFS" );

        case SQLITE_AUTH        :
            return _T( "SQLITE_AUTH" );

        case SQLITE_FORMAT      :
            return _T( "SQLITE_FORMAT" );

        case SQLITE_RANGE       :
            return _T( "SQLITE_RANGE" );

        case SQLITE_ROW         :
            return _T( "SQLITE_ROW" );

        case SQLITE_DONE        :
            return _T( "SQLITE_DONE" );

        case CPPSQLITE_ERROR    :
            return _T( "CPPSQLITE_ERROR" );

        default:
            return _T( "UNKNOWN_ERROR" );
    }
}


QsqlError::~QsqlError()
{



}

/////////////////////////////////////////////////////////////////////////////
// QsqlDatabase

QsqlDatabase::QsqlDatabase()
{
    mpDB = 0;
    mnBusyTimeoutMs = 60000; // 60 seconds
}

QsqlDatabase::QsqlDatabase( const QsqlDatabase & db )
{
    mpDB = db.mpDB;
    mnBusyTimeoutMs = 60000; // 60 seconds
}


QsqlDatabase::~QsqlDatabase()
{


    Close();

}

////////////////////////////////////////////////////////////////////////////////

QsqlDatabase & QsqlDatabase::operator=( const QsqlDatabase & db )
{
    mpDB = db.mpDB;
    mnBusyTimeoutMs = 60000; // 60 seconds
    return *this;
}

void QsqlDatabase::Open( LPCTSTR szFile )
{
    int nRet;

#if defined(_UNICODE) || defined(UNICODE)

    nRet = sqlite3_open16( szFile, &mpDB ); // not tested under window 98

#else // For Ansi Version
//*************-  Added by Begemot  szFile must be in unicode- 23/03/06 11:04 - ****
    OSVERSIONINFOEX osvi;
    ZeroMemory( &osvi, sizeof( OSVERSIONINFOEX ) );
    osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFOEX );
    GetVersionEx ( ( OSVERSIONINFO * ) &osvi );

    if ( osvi.dwMajorVersion == 5 )
    {
        WCHAR pMultiByteStr[MAX_PATH + 1];
        MultiByteToWideChar( CP_ACP, 0, szFile,
                             _tcslen( szFile ) + 1, pMultiByteStr,
                             sizeof( pMultiByteStr ) / sizeof( pMultiByteStr[0] ) );
        nRet = sqlite3_open16( pMultiByteStr, &mpDB );
    }
    else
        nRet = sqlite3_open_v2( szFile, &mpDB, SQLITE_OPEN_READONLY, 0 );

    //nRet = sqlite3_open (szFile, &mpDB );
#endif

//*************************
    if ( nRet != SQLITE_OK )
    {
        LPCTSTR szError = ( LPCTSTR ) _sqlite3_errmsg( mpDB );
        throw QsqlError( nRet, ( LPTSTR )szError, DONT_DELETE_MSG );
    }

    setBusyTimeout( mnBusyTimeoutMs );
}

void QsqlDatabase::Close()
{
    if ( mpDB )
    {
        int nRet = _sqlite3_close( mpDB );

        if ( nRet != SQLITE_OK )
        {
            LPCTSTR szError = ( LPCTSTR )_sqlite3_errmsg( mpDB );
            throw QsqlError( nRet, ( LPTSTR )szError, DONT_DELETE_MSG );
        }

        mpDB = 0;
    }
}


QsqlStatement QsqlDatabase::compileStatement( LPCTSTR szSQL )
{
    checkDB();

    sqlite3_stmt * pVM = compile( szSQL );

    return QsqlStatement( mpDB, pVM );
}


bool QsqlDatabase::tableExists( LPCTSTR szTable )
{
    QString  szSQL ;
    szSQL.Format( _T( "select count(*) from sqlite_master where type='table' and name='%s'" ),	szTable );

    int nRet = execScalar( szSQL );
    return ( nRet > 0 );
}


int QsqlDatabase::execDML( LPCTSTR szSQL )
{
    int nRet;
    sqlite3_stmt * pVM;
    checkDB();

    do
    {
        pVM = compile( szSQL );

        nRet = _sqlite3_step( pVM );

        if ( nRet == SQLITE_ERROR )
        {
            LPCTSTR szError = ( LPCTSTR ) _sqlite3_errmsg( mpDB );
            throw QsqlError( nRet, ( LPTSTR )szError, DONT_DELETE_MSG );
        }

        nRet = _sqlite3_finalize( pVM );
    }
    while ( nRet == SQLITE_SCHEMA );

    return nRet;
}

QsqlQuery QsqlDatabase::execQuery( LPCTSTR szSQL )
{
    checkDB();
    int nRet;
    sqlite3_stmt * pVM;

    do
    {
        pVM = compile( szSQL );

        nRet = _sqlite3_step( pVM );

        if ( nRet == SQLITE_DONE )
        {
            // no rows
            return QsqlQuery( mpDB, pVM, true/*eof*/ );
        }
        else if ( nRet == SQLITE_ROW )
        {
            // at least 1 row
            return QsqlQuery( mpDB, pVM, false/*eof*/ );
        }

        nRet = _sqlite3_finalize( pVM );
    }
    while ( nRet == SQLITE_SCHEMA ); // Edit By Begemot 08/16/06 12:44:35 -   read SQLite FAQ

    LPCTSTR szError = ( LPCTSTR ) _sqlite3_errmsg( mpDB );
    throw QsqlError( nRet, ( LPTSTR )szError, DONT_DELETE_MSG );
}


int QsqlDatabase::execScalar( LPCTSTR szSQL )
{
    QsqlQuery q = execQuery( szSQL );

    if ( q.eof() || q.numFields() < 1 )
        throw QsqlError( CPPSQLITE_ERROR, _T( "Invalid scalar query" ),	DONT_DELETE_MSG );

    return _tstoi( q.fieldValue( 0 ) );
}

// Added By Begemot, exact as execScalar but return String  08/06/06 16:30:37
QString QsqlDatabase::execScalarStr( LPCTSTR szSQL )
{
    QsqlQuery q = execQuery( szSQL );

    if ( q.eof() || q.numFields() < 1 )
        throw QsqlError( CPPSQLITE_ERROR, _T( "Invalid scalar query" ),	DONT_DELETE_MSG );

    return  QString ( q.getStringField( 0 ) );
}

sqlite_int64 QsqlDatabase::lastRowId()
{
    return sqlite3_last_insert_rowid( mpDB );
}


void QsqlDatabase::setBusyTimeout( int nMillisecs )
{
    mnBusyTimeoutMs = nMillisecs;
    sqlite3_busy_timeout( mpDB, mnBusyTimeoutMs );
}


void QsqlDatabase::checkDB()
{
    if ( !mpDB )
        throw QsqlError( CPPSQLITE_ERROR, _T( "Database not open" ), DONT_DELETE_MSG );

}


sqlite3_stmt * QsqlDatabase::compile( LPCTSTR szSQL )
{
    checkDB();
    sqlite3_stmt * pVM;

    int nRet = _sqlite3_prepare( mpDB, szSQL, -1, &pVM, NULL );

    if ( nRet != SQLITE_OK )
    {
        pVM = NULL;
        LPCTSTR szError = ( LPCTSTR ) _sqlite3_errmsg( mpDB );
        throw QsqlError( nRet, ( LPTSTR )szError, DONT_DELETE_MSG );
    }

    return pVM;
}


int QsqlDatabase::GetRowCount( const QString  & table )
{
    QString  str = L"SELECT count(*) FROM " + table;

    QsqlQuery q = execQuery( str );
    return q.getIntField( 0 );
}


//////////////////////// QsqlStatement  ///////////////////////////////////////////
QsqlStatement::QsqlStatement()
{
    mpDB = 0;
    mpVM = 0;
}

QsqlStatement::QsqlStatement( const QsqlStatement & rStatement )
{
    mpDB = rStatement.mpDB;
    mpVM = rStatement.mpVM;
    // Only one object can own VM
    const_cast<QsqlStatement &>( rStatement ).mpVM = 0;
}

QsqlStatement::QsqlStatement( sqlite3 * pDB, sqlite3_stmt * pVM )
{
    mpDB = pDB;
    mpVM = pVM;
}

QsqlStatement::~QsqlStatement()
{
    try
    {
        finalize();
    }
    catch ( ... )	{}
}

QsqlStatement & QsqlStatement::operator=( const QsqlStatement & rStatement )
{
    mpDB = rStatement.mpDB;
    mpVM = rStatement.mpVM;
    // Only one object can own VM
    const_cast<QsqlStatement &>( rStatement ).mpVM = 0;
    return *this;
}

int QsqlStatement::execDML()
{
    checkDB();
    checkVM();

    int nRet = sqlite3_step( mpVM );

    if ( nRet == SQLITE_DONE )
    {
        int nRowsChanged = sqlite3_changes( mpDB );

        nRet = sqlite3_reset( mpVM );

        if ( nRet != SQLITE_OK )
        {
            LPCTSTR szError = ( LPCTSTR ) _sqlite3_errmsg( mpDB );
            throw QsqlError( nRet, ( LPTSTR )szError, DONT_DELETE_MSG );
        }

        return nRowsChanged;
    }
    else
    {
        nRet = sqlite3_reset( mpVM );
        LPCTSTR szError = ( LPCTSTR ) _sqlite3_errmsg( mpDB );
        throw QsqlError( nRet, ( LPTSTR )szError, DONT_DELETE_MSG );
    }
}


void QsqlStatement::bind( int nParam, LPCTSTR szValue )
{
    checkVM();
    int nRes = _sqlite3_bind_text( mpVM, nParam, szValue, -1, SQLITE_TRANSIENT );

    if ( nRes != SQLITE_OK )
        throw QsqlError( nRes, _T( "Error binding string param" ), DONT_DELETE_MSG );
}


void QsqlStatement::bind( int nParam, const int nValue )
{
    checkVM();
    int nRes = sqlite3_bind_int( mpVM, nParam, nValue );

    if ( nRes != SQLITE_OK )
        throw QsqlError( nRes, _T( "Error binding int param" ), DONT_DELETE_MSG );
}


void QsqlStatement::bind( int nParam, const double dValue )
{
    checkVM();
    int nRes = sqlite3_bind_double( mpVM, nParam, dValue );

    if ( nRes != SQLITE_OK )
        throw QsqlError( nRes,	_T( "Error binding double param" ), DONT_DELETE_MSG );
}


void QsqlStatement::bind( int nParam, const unsigned char * blobValue, int nLen )
{
    checkVM();
    int nRes = sqlite3_bind_blob( mpVM, nParam, ( const void * )blobValue, nLen, SQLITE_TRANSIENT );

    if ( nRes != SQLITE_OK )
        throw QsqlError( nRes, _T( "Error binding blob param" ), DONT_DELETE_MSG );
}


void QsqlStatement::bindNull( int nParam )
{
    checkVM();
    int nRes = sqlite3_bind_null( mpVM, nParam );

    if ( nRes != SQLITE_OK )
        throw QsqlError( nRes, _T( "Error binding NULL param" ), DONT_DELETE_MSG );
}


void QsqlStatement::reset()
{
    if ( mpVM )
    {
        int nRet = sqlite3_reset( mpVM );

        if ( nRet != SQLITE_OK )
        {
            LPCTSTR szError = ( LPCTSTR ) _sqlite3_errmsg( mpDB );
            throw QsqlError( nRet, ( LPTSTR )szError, DONT_DELETE_MSG );
        }
    }
}


void QsqlStatement::finalize()
{
    if ( mpVM )
    {
        int nRet = sqlite3_finalize( mpVM );
        mpVM = 0;

        if ( nRet != SQLITE_OK )
        {
            LPCTSTR szError = ( LPCTSTR ) _sqlite3_errmsg( mpDB );
            throw QsqlError( nRet, ( LPTSTR )szError, DONT_DELETE_MSG );
        }
    }
}


void QsqlStatement::checkDB()
{
    if ( mpDB == 0 ) throw QsqlError( CPPSQLITE_ERROR, _T( "Database not open" ), DONT_DELETE_MSG );
}

void QsqlStatement::checkVM()
{
    if ( mpVM == 0 )
        throw QsqlError( CPPSQLITE_ERROR, _T( "Null Virtual Machine pointer" ), DONT_DELETE_MSG );
}


/////////////////////  QsqlQuery  //////////////////////////////////////////////////
QsqlQuery::QsqlQuery()
{
    mpVM = 0;
    mbEof = true;
    mnCols = 0;
    mbOwnVM = false;
}


QsqlQuery::QsqlQuery( const QsqlQuery & rQuery )
{
    mpVM = rQuery.mpVM;
    // Only one object can own the VM
    const_cast<QsqlQuery &>( rQuery ).mpVM = 0;
    mbEof = rQuery.mbEof;
    mnCols = rQuery.mnCols;
    mbOwnVM = rQuery.mbOwnVM;
}


QsqlQuery::QsqlQuery( sqlite3 * pDB, sqlite3_stmt * pVM,
                      bool bEof,	 bool bOwnVM/*=true*/ )
{
    mpDB = pDB;
    mpVM = pVM;
    mbEof = bEof;
    mnCols = _sqlite3_column_count( mpVM );
    mbOwnVM = bOwnVM;
}

QsqlQuery::~QsqlQuery()
{
    try
    {
        finalize();
    }
    catch ( ... ) {}
}


QsqlQuery & QsqlQuery::operator=( const QsqlQuery & rQuery )
{
    try
    {
        finalize();
    }
    catch ( ... )	{ }

    mpVM = rQuery.mpVM;
    // Only one object can own the VM
    const_cast<QsqlQuery &>( rQuery ).mpVM = 0;
    mbEof = rQuery.mbEof;
    mnCols = rQuery.mnCols;
    mbOwnVM = rQuery.mbOwnVM;
    return *this;
}


int QsqlQuery::numFields()
{
    checkVM();
    return mnCols;
}


LPCTSTR QsqlQuery::fieldValue( int nField )
{
    checkVM();

    if ( nField < 0 || nField > mnCols - 1 )
        throw QsqlError( CPPSQLITE_ERROR, _T( "Invalid field index requested" ), DONT_DELETE_MSG );

    return ( LPCTSTR )_sqlite3_column_text( mpVM, nField );
}


LPCTSTR QsqlQuery::fieldValue( LPCTSTR szField )
{
    int nField = fieldIndex( szField );
    return ( LPCTSTR )_sqlite3_column_text( mpVM, nField );
}


int QsqlQuery::getIntField( int nField, int nNullValue/*=0*/ )
{
    if ( fieldDataType( nField ) == SQLITE_NULL )
    {
        return nNullValue;
    }
    else
    {
        return _sqlite3_column_int( mpVM, nField );
    }
}


int QsqlQuery::getIntField( LPCTSTR szField, int nNullValue/*=0*/ )
{
    int nField = fieldIndex( szField );
    return getIntField( nField, nNullValue );
}


double QsqlQuery::getFloatField( int nField, double fNullValue/*=0.0*/ )
{
    if ( fieldDataType( nField ) == SQLITE_NULL )
    {
        return fNullValue;
    }
    else
    {
        return _sqlite3_column_double( mpVM, nField );
    }
}


double QsqlQuery::getFloatField( LPCTSTR szField, double fNullValue/*=0.0*/ )
{
    int nField = fieldIndex( szField );
    return getFloatField( nField, fNullValue );
}


LPCTSTR QsqlQuery::getStringField( int nField, LPCTSTR szNullValue/*=""*/ )
{
    if ( fieldDataType( nField ) == SQLITE_NULL )
    {
        return szNullValue;
    }
    else
    {
        return ( LPCTSTR )_sqlite3_column_text( mpVM, nField );
    }
}


LPCTSTR QsqlQuery::getStringField( LPCTSTR szField, LPCTSTR szNullValue/*=""*/ )
{
    int nField = fieldIndex( szField );
    return getStringField( nField, szNullValue );
}


const unsigned char * QsqlQuery::getBlobField( int nField, int & nLen )
{
    checkVM();

    if ( nField < 0 || nField > mnCols - 1 )
        throw QsqlError( CPPSQLITE_ERROR, _T( "Invalid field index requested" ), DONT_DELETE_MSG );

    nLen = _sqlite3_column_bytes( mpVM, nField );
    return ( const unsigned char * )sqlite3_column_blob( mpVM, nField );
}

int QsqlQuery::getBlobLen( int nField )
{
    checkVM();

    if ( nField < 0 || nField > mnCols - 1 )
        throw QsqlError( CPPSQLITE_ERROR, _T( "Invalid field index requested" ), DONT_DELETE_MSG );

    return _sqlite3_column_bytes( mpVM, nField );
}


const unsigned char * QsqlQuery::getBlobField( LPCTSTR szField, int & nLen )
{
    int nField = fieldIndex( szField );
    return getBlobField( nField, nLen );
}


bool QsqlQuery::fieldIsNull( int nField )
{
    return ( fieldDataType( nField ) == SQLITE_NULL );
}


bool QsqlQuery::fieldIsNull( LPCTSTR szField )
{
    int nField = fieldIndex( szField );
    return ( fieldDataType( nField ) == SQLITE_NULL );
}


int QsqlQuery::fieldIndex( LPCTSTR szField )
{
    checkVM();

    if ( szField )
    {
        for ( int nField = 0; nField < mnCols; nField++ )
        {
            LPCTSTR szTemp = ( LPCTSTR )_sqlite3_column_name( mpVM, nField );

            if ( _tcscmp( szField, szTemp ) == 0 )
            {
                return nField;
            }
        }
    }

    throw QsqlError( CPPSQLITE_ERROR, _T( "Invalid field name requested" ), DONT_DELETE_MSG );
}


LPCTSTR QsqlQuery::fieldName( int nCol )
{
    checkVM();

    if ( nCol < 0 || nCol > mnCols - 1 )
    {
        throw QsqlError( CPPSQLITE_ERROR, _T( "Invalid field index requested" ), DONT_DELETE_MSG );
    }

    return ( LPCTSTR )_sqlite3_column_name( mpVM, nCol );
}


LPCTSTR QsqlQuery::fieldDeclType( int nCol )
{
    checkVM();

    if ( nCol < 0 || nCol > mnCols - 1 )
    {
        throw QsqlError( CPPSQLITE_ERROR, _T( "Invalid field index requested" ), DONT_DELETE_MSG );
    }

    return ( LPCTSTR )_sqlite3_column_decltype( mpVM, nCol );
}


int QsqlQuery::fieldDataType( int nCol )
{
    checkVM();

    if ( nCol < 0 || nCol > mnCols - 1 )
    {
        throw QsqlError( CPPSQLITE_ERROR, _T( "Invalid field index requested" ), DONT_DELETE_MSG );
    }

    return _sqlite3_column_type( mpVM, nCol );
}


bool QsqlQuery::eof()
{
    checkVM();
    return mbEof;
}


void QsqlQuery::nextRow()
{
    checkVM();

    int nRet = _sqlite3_step( mpVM );

    if ( nRet == SQLITE_DONE )
    {
        // no rows
        mbEof = true;
    }
    else if ( nRet == SQLITE_ROW )
    {
        // more rows, nothing to do
    }
    else
    {
        nRet = _sqlite3_finalize( mpVM );
        mpVM = 0;
        LPCTSTR szError = ( LPCTSTR )_sqlite3_errmsg( mpDB );
        throw QsqlError( nRet,	( LPTSTR )szError, DONT_DELETE_MSG );
    }
}


void QsqlQuery::finalize()
{
    if ( mpVM && mbOwnVM )
    {
        int nRet = _sqlite3_finalize( mpVM );
        mpVM = 0;

        if ( nRet != SQLITE_OK )
        {
            LPCTSTR szError = ( LPCTSTR )_sqlite3_errmsg( mpDB );
            throw QsqlError( nRet, ( LPTSTR )szError, DONT_DELETE_MSG );
        }
    }
}

void QsqlQuery::checkVM()
{
    if ( mpVM == 0 )
    {
        throw QsqlError( CPPSQLITE_ERROR, _T( "Null Virtual Machine pointer" ), DONT_DELETE_MSG );
    }
}


////////////////////////////////////////////////////////////////////////////////
//**************************
//*************-  Added By Begemot - 28/02/06 20:25 - ****
QString DoubleQuotes( QString in )
{
    in.Replace( _T( "\'" ), _T( "\'\'" ) );
    return in;
}





//
//
//
//
//
//
//
//QsqlDatabase2::QsqlDatabase2()
//{
//
//}
//
//
//QsqlDatabase2::~QsqlDatabase2()
//{
//	finishQ();
//
//}
//int QsqlDatabase2::readFileQ( const char * path, spmembuffer_t * mem )
//{
//	int ret = -1;
//
//	FILE * fp = fopen( path, "rb" );
//
//	if( NULL != fp )
//	{
//		struct stat filestat;
//
//		if( 0 == stat( path, &filestat ) )
//		{
//			ret = 0;
//
//			mem->total = mem->used = filestat.st_size;
//			mem->data = (char*)malloc( filestat.st_size + 1 );
//			fread( mem->data, filestat.st_size, 1, fp );
//			(mem->data)[ filestat.st_size ] = '\0';
//		}
//		else
//		{
//			printf( "cannot stat file %s\n", path );
//		}
//
//		fclose( fp );
//	}
//	else
//	{
//		printf( "cannot open file %s\n", path );
//	}
//
//	return ret;
//}
//
//
//
//
//void QsqlDatabase2::initQ(const char * path )
//{
//
//
//	spmemvfs_env_init();
//
//
//
//	spmembuffer_t * mem = (spmembuffer_t*)calloc( sizeof( spmembuffer_t ), 1 );
//
//	readFileQ( path, mem );
//	spmemvfs_open_db( &db, path, mem );
//
//
//
//	ASSERT( db.mem == mem );
//}
//
//
//
//void QsqlDatabase2::finishQ()
//{
//	spmemvfs_close_db( &db );
//	spmemvfs_env_fini();
//}
//
//QsqlQuery QsqlDatabase2::execQuery(LPCTSTR szSQL)
//{
//	//checkDB();
//	int nRet;
//	sqlite3_stmt* pVM;
//
//	do{
//		pVM = compile(szSQL);
//
//		nRet = _sqlite3_step(pVM);
//
//		if (nRet == SQLITE_DONE)
//		{	// no rows
//			return QsqlQuery(db.handle, pVM, true/*eof*/);
//		}
//		else if (nRet == SQLITE_ROW)
//		{	// at least 1 row
//			return QsqlQuery(db.handle, pVM, false/*eof*/);
//		}
//		nRet = _sqlite3_finalize(pVM);
//	}
//	while( nRet == SQLITE_SCHEMA ); // Edit By Begemot 08/16/06 12:44:35 -   read SQLite FAQ
//
//	LPCTSTR szError = (LPCTSTR) _sqlite3_errmsg(db.handle);
//	throw QsqlError(nRet, (LPTSTR)szError, DONT_DELETE_MSG);
//}
//
//
//sqlite3_stmt* QsqlDatabase2::compile(LPCTSTR szSQL)
//{
//	//checkDB();
//	sqlite3_stmt* pVM;
//
//	int nRet = _sqlite3_prepare(db.handle , szSQL, -1, &pVM, NULL);
//
//	if (nRet != SQLITE_OK)
//	{
//		pVM=NULL;
//		LPCTSTR szError = (LPCTSTR) _sqlite3_errmsg(db.handle);
//		throw QsqlError(nRet, (LPTSTR)szError, DONT_DELETE_MSG);
//	}
//	return pVM;
//}




 
#endif